const hre = require("hardhat");
const utils = require("./utils");
module.exports = {
    
    /**
     * 发布token
     */
    async pubToken(forLiquidity, forSwap, params){
        // signer发合约的地址
        console.log('\x1B[37m', 'forLiquidity:', forLiquidity.address)
        console.log('forSwap:', forSwap.address)

        console.log('-------------- frc758 --------------')
        const { tokenA, tokenB } = await this.frc758([forLiquidity, forSwap], params)
        const mint = await tokenA.timeBalanceOf(forLiquidity.address, params.startTime, params.endTime); // startTime 必须大于当前时间
        const mint2 = await tokenB.timeBalanceOf(forLiquidity.address, params.startTime, params.endTime); // startTime 必须大于当前时间

        console.log('-------------- frc758 --------------')
        return { tokenA, tokenB }
    },

    
   async frc758 (_s, config)  {
        const signers = _s[0];
        const forSwap = _s[1];
    
        const FRC758 = await hre.ethers.getContractFactory("ChaingeToken");
    
        const tokenA = await FRC758.deploy("TokenA", "F1", 18, 10000);
        await tokenA.deployed();
 
        await utils.sleep();


        await tokenA.mintTimeSlice(signers.address, config.amountA, config.startTime, config.endTime)
        // await tokenA.mintTimeSlice(forSwap.address, config.amountA, config.startTime, config.endTime)
        console.log('toeknA address:', tokenA.address)
        const bal = await tokenA.timeBalanceOf(signers.address, config.startTime, config.endTime); // startTime 必须大于当前时间
        console.log('tokenA balance:', parseInt(bal._hex))
    

        //转账给forSwap
        await tokenA.timeSliceTransferFrom(signers.address,forSwap.address ,30000000,config.startTime, config.endTime); // startTime 必须大于当前时间
        

        const forSwapBal = await tokenA.timeBalanceOf(forSwap.address, config.startTime, config.endTime); // startTime 必须大于当前时间
        console.log('forSwapBal tokenA balance:', parseInt(forSwapBal._hex))


        const bal_two = await tokenA.timeBalanceOf(signers.address, config.startTime, config.endTime); // startTime 必须大于当前时间
        console.log('tokenA balance:', parseInt(bal_two._hex))



        // let balanceOfA =  await tokenA.balanceOf(signers.address);   
        // console.log('balanceOfA',balanceOfA);
        await utils.sleep()
    
        const tokenB = await FRC758.deploy("TokenB", "F2", 18, 10000);
        await tokenB.deployed();
        await utils.sleep()
        await tokenB.mintTimeSlice(signers.address, config.amountB, config.startTime, config.endTime)
        // await tokenB.mint(forSwap.address, config.amountB)
        console.log('toeknB address:', tokenB.address)
        const bal2 = await tokenB.timeBalanceOf(signers.address, config.startTime, config.endTime); // startTime 必须大于当前时间
        console.log('tokenB balance:', parseInt(bal2._hex))
    
        return {
            tokenA,
            tokenB
        }
    },

    //对合约进行授权
    async approve (routerAddress, tokenA, tokenB){
        // console.log("approve start");
        // 给路由合约授权
        await tokenA.setApprovalForAll(routerAddress, true)
        // console.log("approve end");
        await utils.sleep()
        // console.log("approve start");
        await tokenB.setApprovalForAll(routerAddress, true)
        // console.log("approve end");
    },

    //检查余额
    async checkBalance(signers, tokenA, tokenB, config) {
        const balanceA = await tokenA.timeBalanceOf(signers.address, config.startTime, config.endTime)
        const balanceB = await tokenB.timeBalanceOf(signers.address, config.startTime, config.endTime)
    
        console.log('A:', parseInt(balanceA._hex), 'B:', parseInt(balanceB._hex),config.startTime, config.endTime,signers.address);
        return [parseInt(balanceA._hex), parseInt(balanceB._hex)]
    }
  
};
  